﻿using System;
using System.Collections.Generic;
using System.IO;
using CrateMayhem.Common.Data;

namespace CrateMayhem.Desktop.Data
{
    public class SettingsRepository : SettingsRepositoryBase
    {
        private Dictionary<string, object> properties;
        private string path;

        protected override IDictionary<string, object> Properties
        {
            get
            {
                if (properties == null)
                {
                    properties = new Dictionary<string, object>();
                    path = Path.Combine(Environment.CurrentDirectory, "Properties.txt");

                    if (File.Exists(path))
                    {
                        using (var reader = new StreamReader("Properties.txt"))
                        {
                            var entry = reader.ReadLine();

                            while (entry != null)
                            {
                                var data = entry.Split('|');
                                Properties[data[0]] = data[1];
                                entry = reader.ReadLine();
                            }
                        }
                    }
                }

                return properties;
            }
        }

        protected override void Save()
        {
            using (var writer = new StreamWriter("Properties.txt", false))
            {
                foreach (var prop in Properties)
                {
                    writer.WriteLine("{0}|{1}", prop.Key, prop.Value);
                }
            }
        }
    }
}